﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebAppDemo.CookieSession
{
    public partial class ReadCookie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie hc = Request.Cookies.Get("ASP.NET_SessionId");
            if (hc != null)
            {
                lblSessionId.Text = hc.Value;
            }
        }

        protected void btnShowCookie_Click(object sender, EventArgs e)
        {
            HttpCookie ht = Request.Cookies.Get("CurrentUser");
            if (ht!=null)
            {
                lblShow.Text = ht.Value;
            }
            else
            {
                lblShow.Text = "还没有存储Cookie";
            }
        }

        protected void btnSetCookie_Click(object sender, EventArgs e)
        {
            HttpCookie hc = new HttpCookie("CurrentUser");
            hc.Value = System.DateTime.Now.ToShortTimeString();
            hc.Expires = System.DateTime.Now.AddHours(1);
            hc.Domain = "www.blog.surveyol.com";
            Response.Cookies.Add(hc);
        }

        protected void btnClearCookie_Click(object sender, EventArgs e)
        {
            HttpCookie ht = Request.Cookies.Get("CurrentUser");
            if (ht!=null)
            {
                ht.Expires = System.DateTime.Now.AddDays(-1);
                Response.Cookies.Add(ht);
            }
            
        }
    }
}